<?php

namespace Admin\Controller;

use Think\Controller;

class IndexController extends CommonController
{
    /**
     *  主页面
     */
    public function index()
    {
        //公告前三条
        $notice = M('notice', '', 'KEFU')->where(['type' => 1, 'status' => 1])->limit(3)->order('id desc')->field('id,title')->select();
       //三天之内高亮公告
//        $three=M('')
        //工单列表
        $order = M('work_order', '', 'KEFU')->field('id,status,publish_name,accept_name,ctime,title,accept_apart_id')->limit(0, 5)->order('status asc,ctime desc')->select();
        foreach ($order as $k => $v) {
            $order[$k]['ctime'] = date('Y-m-d H:i', $v['ctime']);
            switch ($v['status']) {
                case 1:
                    $order[$k]['status'] = '待处理';
                    break;
                case 2:
                    $order[$k]['status'] = '已回复';
                    break;
                case 3:
                    $order[$k]['status'] = '已完成';
                    break;
                case 4:
                    $order[$k]['status'] = '关闭';
                    break;
                case 5:
                    $order[$k]['status'] = '删除';
                    break;
                default:
                    $order[$k]['status'] = '待处理';
                    break;
            }
            //部门
            $part=M('apart','','KEFU')->where(['id'=>$v['accept_apart_id']])->getField('name');
            $order[$k]['apart']=$part;
        }

        $agent = M('admin', '', 'KEFU')->where(['id' => $_SESSION['id']])->Field('agent,agent_status,login_sys')->find();
        $check_time=M('anti_check_user','','KEFU')->where(['status'=>9,'out_kf_id'=>$_SESSION['id']])->order('ctime asc')->limit(1)->field('ctime')->find();
        if(!empty($check_time)){
            $check_time=$check_time['ctime']+3600;
            $task_time=date('Y-m-d H:i:s', $check_time);
            $time=time();
            $day = floor(abs(($time - $check_time))/ (24 * 3600));//天数
            $hour_time = abs(($time - $check_time) )- $day * (24 * 3600);//小时时间戳
            $hour = floor($hour_time / 3600);
            if($time<$check_time){
                $time_s='距离任务还有'.$day.'天'.$hour.'小时';
            }else{
                $time_s='任务超时'.$day.'天'.$hour.'小时';
            }
        }else{
            $task_time="";
        }

        if(!empty($agent['agent'])){
            $number=explode('@',$agent['agent']);
            $number=$number[0];
        }else{
            $number= $_SESSION['id'];
        }

        $this->assign(array(
            'check_time'=>$task_time,
            'time_s'=>$time_s,
            'notice' => $notice,
            'agent' => $agent['agent'],
            'agent_status' => $agent['agent_status'],
            'login_sys' => $agent['login_sys'],
            'order' => $order,
            'admin_id' => $_SESSION['id'],
            'role_id' => $_SESSION['role_id'],
            'number' =>$number,
        ));
        $this->display();
    }

    //分页获取工单
    public function get_order()
    {
        //工单列表
        $page = I('page', 1);
        $first = ($page - 1) * 5;
        $order = M('work_order', '', 'KEFU')->field('id,status,publish_name,accept_name,ctime,title,accept_apart_id')->limit($first, 5)->order('status asc,ctime desc')->select();
        foreach ($order as $k => $v) {
            $order[$k]['ctime'] = date('Y-m-d H:i', $v['ctime']);
            switch ($v['status']) {
                case 1:
                    $order[$k]['status'] = '待处理';
                    break;
                case 2:
                    $order[$k]['status'] = '已回复';
                    break;
                case 3:
                    $order[$k]['status'] = '已完成';
                    break;
                case 4:
                    $order[$k]['status'] = '关闭';
                    break;
                case 5:
                    $order[$k]['status'] = '删除';
                    break;
                default:
                    $order[$k]['status'] = '待处理';
                    break;
            }
            //部门
            $part=M('apart','','KEFU')->where(['id'=>$v['accept_apart_id']])->getField('name');
            $order[$k]['apart']=$part;
        }
        $this->ajaxReturn(['code' => '1001', 'order' => $order]);
    }

    /**
     *  工单 公告信息+工单动态
     */
    public function noticeOrder()
    {
        //公告前三条
        $notice = M('notice', '', 'KEFU')->where(['type' => 1, 'status' => 1])->limit(3)->order('id desc')->field('id,title')->select();
        //工单列表
        $where = [];
//        $where['status'] = array('in', array(1, 2));
        $order = M('work_order', '', 'KEFU')->where($where)->field('id,publish_name,accept_name,ctime,title,status,accept_apart_id')->limit(0, 5)->order('status asc,ctime desc')->select();
        foreach ($order as $k => $v) {
            $order[$k]['ctime'] = date('Y-m-d H:i', $v['ctime']);
            switch ($v['status']) {
                case 1:
                    $order[$k]['status'] = '待处理';
                    break;
                case 2:
                    $order[$k]['status'] = '已回复';
                    break;
                case 3:
                    $order[$k]['status'] = '已完成';
                    break;
                case 4:
                    $order[$k]['status'] = '关闭';
                    break;
                case 5:
                    $order[$k]['status'] = '删除';
                    break;
                default:
                    $order[$k]['status'] = '待处理';
                    break;
            }
            //部门
            $part=M('apart','','KEFU')->where(['id'=>$v['accept_apart_id']])->getField('name');
            $order[$k]['apart']=$part;
        }
        $this->ajaxReturn(['code' => '1001', 'notice' => $notice, 'order' => $order]);
    }

    /**
     *   用户信息
     */
    public function get_user()
    {
//        $phone = I('phone');
//        $where = [];
//        if (strlen($phone) < 11) {
//            $where['id'] = $phone;
//        } else {
//            $where['mobile'] = $phone;
//        }
//
//        $data = M('user', '', 'USER_R')->where($where)->field('id as user_id,vid,truename,mobile,cert_status,ctime,sex,type')->find();
//        $first = substr($data['mobile'], 0, 3);
//        $end = substr($data['mobile'], 7, 4);
//        $data['user_mobile'] = $first . '****' . $end;
//        $data['mobile_firm']=M('user_login','','USER')->where(['vid'=>$data['vid']])->getfield('mobilefirm');
//
//        //用户所属
//        switch ($data['type']){
//            case 1:  $data['user_type']='共享现金'; break;
//            case 2:  $data['user_type']='鼎信贷'; break;
//            case 3:  $data['user_type']='马上游呗'; break;
//        }
//
//        if (empty($data)){
//            $this->ajaxReturn(['code' => '1002', 'result' => '']);
//            die;
//        }
//
//        $data['ctime'] = date('Y-m-d H:i', $data['ctime']);
//
//        if ($data['sex'] == '1') {
//            $data['sex'] = '男';
//        } else if ($data['sex'] == '2') {
//            $data['sex'] = '女';
//        } else if ($data['sex'] == '0') {
//            $data['sex'] = '未知';
//        }
//
//        //基础认证，1未认证，2已通过，3未通过4.认证中,5审核中
//        if ($data['cert_status'] == 2) {
//            $data['cert_status'] = '通过';
//        } else if($data['cert_status'] == 1) {
//            $data['cert_status'] = '未认证';
//        }else if($data['cert_status'] == 3) {
//            $data['cert_status'] = '未通过';
//        }else if($data['cert_status'] ==4) {
//            $data['cert_status'] = '认证中';
//        }else if($data['cert_status'] == 5) {
//            $data['cert_status'] = '审核中';
//        }else if($data['cert_status'] == 6) {
//            $data['cert_status'] = '待审核';
//        }
//        $rec_data = [];
//        //最近联系时间
//        $upd_time = M('user_feedback', '', 'KEFU')->where(['vid' => $data['vid']])->limit(1)->order('upd_time desc')->field('upd_time')->find();
//        if (!empty($upd_time)) {
//            $data['recent_contact'] = date('Y-m-d H:i:s', $upd_time['upd_time']);
//        } else {
//            $data['recent_contact'] = "--:--:--";
//        }
//        //来电记录
//        $call_record = M('call_info', '', 'KEFU')->where(['vid' => $data['vid']])->count();
//        $rec_data['call_record'] = $call_record;
//        //在线反馈
//        $feedback_count = M('user_feedback', '', 'KEFU')->where(['vid' => $data['vid']])->count();
//        $rec_data['feedback_rec'] = $feedback_count;
//        //调查记录
//        $surve_count = M('tmp_user_mark', '', 'KEFU')->where(['vid' => $data['vid']])->count();
//        $rec_data['surve_rec'] = $surve_count;
//        //催收记录
//        $urge_count = M('user_overdue', '', 'URGE')->join('a join dxd_urge.mark b on a.repayment_no=b.repayment_no')->where(['a.vid' => $data['vid']])->field('a.vid')->select();
//        $rec_data['urge_rec'] = count($urge_count);
//        //借款记录
//        $borrow_count = M('user_borrow', '', 'CAIWU')->where(['vid' => $data['vid'],'is_success'=>2])->count();
//        $rec_data['borrow_rec'] = $borrow_count;
//        //还款记录
//        $repay_count = M('user_repayment', '', 'CAIWU')->where(['vid' => $data['vid'],'status'=>3])->count();
//        $rec_data['repay_rec'] = $repay_count;
//        //分期记录
//        $stages_count = M('user_by_stages', '', 'CAIWU')->where(['vid' => $data['vid'],'status'=>['neq',4]])->count();
//        $rec_data['stages_rec'] = $stages_count;
//        //逾期记录
//        $overdue_count = M('user_overdue', '', 'URGE')->where(['vid' => $data['vid']])->count();
//        $rec_data['overdue_rec'] = $overdue_count;
//        //额度变动记录
//        $quota_count=M('user_quota_record','','CAIWU')->where(['vid'=>$data['vid']])->count();
//        $rec_data['quota_count'] = $quota_count;
//        //部分还款
//        $some_pay_count = M('part_repayment', '', 'CAIWU')->where(['vid' => $data['vid']])->count();
//        $rec_data['part_rec'] = $some_pay_count;
//        //第三方支付记录
//        $ali_count = M('alipay_record', '', 'CWXX')->where(['vid' => $data['vid']])->count();
//        $jd_count = M('jd_record', '', 'CWXX')->where(['vid' => $data['vid']])->count();
//        $jh_count = M('jh_record', '', 'CWXX')->where(['vid' => $data['vid']])->count();
//        $llzf_count = M('llzf_record', '', 'CWXX')->where(['vid' => $data['vid']])->count();
//        $third_count = $ali_count + $jd_count + $jh_count + $llzf_count;
//        $rec_data['third_rec'] = $third_count;
//        //逾期罚金减免记录
//        $fine_count = M('user_overdue', '', 'URGE')->join('a join dxd_urge.fine_modify_apply b on a.repayment_no=b.repayment_no')->where(['a.vid' => $data['vid']])->count();
//        $rec_data['reduce_pay_rec'] = $fine_count;
        $this->ajaxReturn(['code' => '1001',  'result' => '']);
    }

    /**
     *  同事
     */
    public function teammates()
    {
        $page = I('page', 1);
        $first = ($page - 1) * 15;
        $data = M('admin', '', 'KEFU')->where(['status' => 1,'out_kf_id'=>0])->field('agent,id,name,status,agent_status,level as role_name')->limit($first, 15)->order('agent_status asc')->select();
        foreach ($data as $k=>$v){
            if($v['agent_status']==0){
                $data[$k]['agent_status']='1';
            }
            $agent=explode('@',$v['agent']);
            $data[$k]['agent_s']=$agent[0];
        }

        if (empty($data)){
            $this->ajaxReturn(['code' => '1002', 'result' => $data]);
            die;
        }
        $this->ajaxReturn(['code' => '1001', 'result' => $data]);
    }

    /**
     *  任务
     */
    public function task()
    {
        $this->display();
    }

    /**
     *  修改坐席状态
     */
    public function change_status()
    {
        $status = I('status');
        $update = M('admin', '', 'KEFU')->where(['id' => $_SESSION['id']])->setField('agent_status', $status);
        if ($update) {
            $this->ajaxReturn(['code' => '1001', 'result' => 'success']);
            die;
        } else {
            $this->ajaxReturn(['code' => '1002', 'result' => 'success']);
            die;
        }
    }

    /**
     *  弹屏用户信息获取
     */
    public function get_call_user()
    {
        $phone = I('phone');
        $user ='';
        $recent_time = time() - (24 * 3600 * 30);//近一个月
        if (!empty($user)) {
            //sex
            if($user['sex']=='1'){
                $user['sex']='男';
            }else if($user['sex']=='2'){
                $user['sex']='女';
            }else{
                $user['sex']='未知';
            }
            //来电记录
            $where = [];
            $where['ctime'] = array('gt', $recent_time);
            $where['vid'] = $user['vid'];
            $where['calltype'] = 2;
            $call_in = M('call_info', '', 'KEFU')->where($where)->count();
            $user['call_in'] = $call_in;
            //外呼去电记录
            $w = [];
            $w['ctime'] = array('gt', $recent_time);
            $w['vid'] = $user['vid'];
            $w['calltype'] = 1;
            $call_out = M('call_info', '', 'KEFU')->where($w)->count();
            $user['call_out'] = $call_out;

            //在线反馈
            $w1 = [];
            $w1['ctime'] = array('gt', $recent_time);
            $w1['vid'] = $user['vid'];
            $feedback_count = M('user_feedback', '', 'KEFU')->where($w1)->count();
            $user['feedback_rec'] = $feedback_count;
            //用户注册时间
            $user['ctime'] = date('Y-m-d H:i:s',$user['ctime']);
            $this->ajaxReturn(['code' => '1001', 'is_jyy' => 1, 'data' => $user]);
            die;

        } else {
            $where = [];
            $where['ctime'] = array('gt', $recent_time);
            $where['callno'] = $phone;
            $where['calltype'] = 2;
            //来电记录
            $call_in = M('call_info', '', 'KEFU')->where($where)->count();
            $rec_data['call_in'] = $call_in;
            //外呼去电记录
            $w = [];
            $w['ctime'] = array('gt', $recent_time);
            $w['calledno'] = $phone;
            $w['calltype'] = 1;
            $call_out = M('call_info', '', 'KEFU')->where($w)->count();
            $rec_data['call_out'] = $call_out;
            $rec_data['phone']=$phone;
            $this->ajaxReturn(['code' => '1001', 'is_jyy' => 2, 'data' => $rec_data]);
            die;
        }
    }

//    返回时间
    public function year_month_day($time)
    {
        $now = time();
        $str = "";
        $has_count = 0;
        $year = floor(($now - $time) / (24 * 3600 * 356));
        if ($year >= 1) {
            $has_count += (24 * 3600 * 356) * $year;
            $str += $year . '年';
            $month = floor(($now - $has_count) / (24 * 3600 * 30));

        } else {
            $month = floor(($now - $time) / (24 * 3600 * 30));

        }
        if ($month >= 1 && $month <= 12) {
            $has_count += (24 * 3600 * 30) * $month;
            $str += $month . '个月';
            $day = floor(($now - $has_count) / (24 * 3600));
        } else {
            $day = floor(($now - $time) / (24 * 3600));
        }
        if ($day >= 1 && $day <= 30) {
            $str += $day . '天';
        } else if ($year == 0 && $month == 0) {
            $str = '未满一天';
        }
        return $str;
    }


}